<?php
    $this->headTitle($this->site_name.' - Admin Clients');
?>

<script type="text/javascript" src="/js/ui/jquery.ui.core.min.js"></script>
<script type="text/javascript" src="/js/ui/jquery.ui.widget.min.js"></script>
<script type="text/javascript" src="/js/ui/jquery.ui.mouse.min.js"></script>
<script type="text/javascript" src="/js/ui/jquery.ui.draggable.min.js"></script>
<script type="text/javascript" src="/js/ui/jquery.ui.droppable.min.js"></script>
<script type="text/javascript" src="/js/ui/jquery.ui.sortable.min.js"></script>
<script type="text/javascript" src="/js/jquery.validate.js"></script>

<style>
	.draggable1, .draggable2 { cursor:pointer;}
	.sortable { list-style-type: none; margin: 0; padding: 0;  }
	.sortable li { margin: 0 3px 3px 3px;  }
	.sortable li a { position: absolute;  }
	input{ float: left; margin-top : 4px }
	select{  margin-top : 4px }
	input[type="checkbox"]{ float: left; margin : 5px }
	label { float:left; }
	div .left-column {width:45%;float:left;margin-left:20px;}
</style>

<script>
<?php
	echo "var page_uri='".$_SERVER['REQUEST_URI']."';\n";
	echo "var services=new Array();\n";
	echo "var departments=new Array();\n";
	echo "var serviceurl=new Array();\n";
	$all_services = $this->all_services;
	foreach ($all_services as $key => $value){
		echo "services['".$value['dpt_id']."']='".$value['icon']."';\n";
		echo "departments['".$value['dpt_id']."']='".$value['dpt_name']."';\n";
		echo "serviceurl['".$value['dpt_id']."']='".$value['service_url']."';\n";
	} 
?>

	function addNewClient(){
    	$("#username").val('');
    	$("#password").val('');
    	
		$("span.ui-dialog-title").text('Add New Client');  		
		$("#submit").val('Add Client');
		$("#client-dialog").dialog("open");
	}		

	function setManager(id){
		$("input[id^='dpt_id_']").attr('checked',false);
	    $.post("/default/do/get-manager", {id: ""+id+""}, function(data)
	    	   	{
	                if(data['client_id']!='') {
	        	    	$("#account_email").val(data['email']);
	        	    	$("#manager_client_id").val(data['client_id']);

	        	    	for (var i=0; i<data['dpt_ids'].length; i++){
		        	    	$("#dpt_id_"+data['dpt_ids'][i]).attr('checked', true);
	        	    	}	        	    	

	                	//alert(content);
	        			$("span.ui-dialog-title").text('Set Manager');  		
	        			$("#submit").val('Set');
	        			$("#client-dialog").dialog("open");
	                }else{
	        	        alert("Sorry, failed to fetch user information.");
	        		}
	    	    }, "json");

	}		
	
	function slide(id){
		if ($("#"+id).hasClass('down')){
			$("#"+id).removeClass('down');
			$("#"+id+"_expand").html(' + ');
			$("#"+id).hide();
		}else{
			$("#"+id).addClass('down');
			$("#"+id+"_expand").html(' - ');
			$("#"+id).show();
		}
	}

	function disableClient(id){
		location=page_uri+'?act=disable&id='+id;
	}

	function suspendClient(id){
		location=page_uri+'?act=suspend&id='+id;
	}
	
	function activateClient(id){
		location=page_uri+'?act=activate&id='+id;
	}
</script>

<script type="text/javascript">
	$(document).ready(function(){

		$('.noscroll').click(function(event) {
			event.preventDefault();
		});

		
	    $("#serviceForm").validate({
	      rules: {
	        username: {
		      required: true
		    },
		    password: {
			  required: true
			}
	      },
	      messages: {
	        username: {
	          required: "account id can not be empty"
	        },
	        password: {
		      required: "password can not be empty"
		    }
	      }
	    });

		$("#client-dialog").dialog({autoOpen:false,modal:true,width:500});
		$( "#sortable1" ).sortable();
		$( "#sortable1" ).disableSelection();
		
		$( ".draggable2" ).draggable({revert: true});		  
	    
	  });

</script>  

<?php if (!empty($this->success_msg)):?>
<div class="response-msg success ui-corner-all"><?php echo $this->success_msg;?></div>
<?php endif ?>

<?php if (!empty($this->error_msg)):?>
<div class="response-msg error ui-corner-all"><?php echo $this->error_msg;?></div>
<?php endif ?>


<div id="admin-dashboard">

<h2>Clients Management</h2>
 
<div  style="margin-top:-16px;border-bottom:1px solid #ccc;"><!-- search form starts here-->

<form action="/admin/client" method="post" name="search_account_form">
<table>
	<tr>
		<td width="20%" align=left valign=middle>Account Email:<?php echo $this->form2->email;?></td>
		<td width="17%" align=left valign=middle>DPT id:<br><?php echo $this->form2->dpt_id;?></td>
		<td width="17%" align=left valign=middle>Login Account ID:<br><?php echo $this->form2->dpt_account_login;?></td>
    	<td width="17%" align=left valign=middle>Login Area:<br><?php echo $this->form2->login_area;?></td>
    	<td width="17%" align=left valign=middle>Role:<br><?php echo $this->form2->role;?></td>
	    <td widht="12%"><?php echo $this->form2->search;?></td>
	</tr>
</table>
</form>
</div>
<!-- search form ends here-->

 
 
<div style="float:left; width:98%; margin:0 0 20px 4px;">
<h3>All Clients: <!--  a href="#" class="grey-btn" style="float:right;display:inline;" onclick="addNewClient();"><span class="ui-title">Add New Client</span></a--></h3>

<div  id="droppable1" style="font-size:12px;font-weight:bold;">		

	<ul id="sortable1">
<?php if (count($this->all_clients)>0):?>
	<?php 
	$i=$this->start;
	foreach($this->all_clients as $index => $item ){
	//foreach ($this->paginator as  $index => $item){
		$i++;
		$status=$item['status'];
		switch($status){
			case 'Pending': $status_str='<div class=left style="margin:-3px 0 0 20px;"><a class=btn onclick="if (confirm(\'Are you sure to activate this client?\n'.$item['email'].'\')) { activateClient(\''.$index.'\');}">Activate</a></div><div class=left style="margin:-3px 0 0 20px;"><a class=btn onclick="if (confirm(\'Are you sure to suspend this client?\n'.$item['email'].'\')) { suspendClient(\''.$index.'\');}">Suspend</a></div>'; break;
			case 'Active': $status_str='<div class=left style="margin:-3px 0 0 20px;"><a class=btn onclick="if (confirm(\'Are you sure to suspend this client?\n'.$item['email'].'\')) { suspendClient(\''.$index.'\');}">Suspend</a></div>'; break;
			case 'Suspend': $status_str='<div class=left style="margin:-3px 0 0 20px;"><a class=btn onclick="if (confirm(\'Are you sure to activate this client?\n'.$item['email'].'\')) { activateClient(\''.$index.'\');}">Activate</a></div>'; break;
			default: $status='';break;
		}
		
		if ($item['role']=='manager')
			$role_str='<div style="margin:4px 5px 0 0;float:left;"><img src="/images/Check-32.png" height=16></div>';
		else
			$role_str='';
		
		//echo '<li title="Click to edit" style="padding:0;"><div>'.$i.') Client Email:  <a href="/admin/client/edit/id/'.$index.'">'.$item['email'].'</a>';
		echo '<li title="Click to edit" style="padding:0;"><div><div class=left style="width:360px;">'.$i.') Client Email:  <a href="/admin/index/to-client/id/'.$index.'" target=_blank>'.$item['email'].'</a></div><div class=left style="width:100px">status:'.$item['status'].'</div>'.$status_str.'<div style="float:right;">'.$role_str.'<div class=left><a class=btn onclick="setManager(\''.$index.'\');">Manager</a></div></div>';
		echo '<div class="clearfix"></div><div>&nbsp;&nbsp;&nbsp;&nbsp;<b id="'.$index.'_expand"> + </b><a href="javascript:void();" class="noscroll" onclick="slide(\''.$index.'\');">Services: '.count($item['services']).'</a></div>';
		echo '<ul id="'.$index.'" style="display:none;">';
		foreach($item['services'] as $key => $value){
			echo '<li id="'.$value['icon'].'" style="float:left;margin:10px;display:inline;">';
			//echo '<div><a href="'.$value['service_url'].'" target=_blank>'.$value['dpt_name'].'</a><span></span><p>'.$value['dpt_id'].'-'.$value['dpt_account_id'].'</p></div>';
			//echo '<div><a href="/client/index/to-service/did/'.$value['dpt_id'].'/aid/'.$value['dpt_account_id'].'/cid/'.$value['client_id'].'" target=_blank>'.$value['dpt_name'].'</a><span></span><p><a style="color:black;" href="/client/index/to-service/did/'.$value['dpt_id'].'/aid/'.$value['dpt_account_id'].'/cid/'.$value['client_id'].'" target=_blank>'.$value['dpt_account_login'].'</a></p></div>';
			echo '<div><a href="/admin/index/to-client/id/'.$value['client_id'].'" target=_blank>'.$value['dpt_name'].'</a><span></span><p><a style="color:black;" href="/admin/index/to-client/id/'.$value['client_id'].'" target=_blank>'.$value['dpt_account_login'].'</a></p></div>';
			echo '</li>';
		}
		echo '</ul>';
		echo '</div>';
		echo '</li>';
		echo '<div class="clearfix" style="margin-bottom:10px;"></div>';
	}
	?>
		
<?php else: ?>
	<li>No client right now.</li>
<?php endif ?>
	</ul>

</div>
 
</div>

<div align=center style="margin:10px 0;"><?php echo $this->paginator; ?></div>


<div id="client-dialog" title="Set Manager" style="display:none;" >
            <?php if (!empty($this->formError)): ?>
            <div class="form_errors_block">
                <?php echo $this->formError; ?>
            </div>
            <?php endif; ?>

            <?php echo $this->form; ?>
</div>


</div>